// 创建birds.js
var express = require('express')
var cookieApi = express.Router()// 创建一个路由实例



// 中间件功能
cookieApi.use(function timeLog(req, res, next) {
  console.log('Time: ', new Date().toLocaleString())
  next()
})
// 访问/putApi
cookieApi.get('/', function (req, res) {

  console.log('zdy-----baseUrl：------>' + req.baseUrl)
  console.log('zdy-----headers：------>' + JSON.stringify(req.headers))
  console.log('zdy-----body：------>' + JSON.stringify(req.body))
  res.send(JSON.stringify(req.body))
})

// 访问/getPlus/:id
cookieApi.get('/cookieTest', function (req, res) {
  // 获取参数
  let headers = req.headers ? JSON.stringify(req.headers) : '';
  let params = req.params ? JSON.stringify(req.params) : '';
  let query = req.query ? JSON.stringify(req.query) : '';
  console.log('params :----> ', params)
  console.log('query   :----> ', query)
  console.log('headers   :----> ', headers)
  res.json({
    header: headers,
    params: params,
    query: query,
    method: req.method,
    url: req.originalUrl,
    msg: '服务器回复'
  })

})
// 访问/getPlus/:id
cookieApi.get('/cookieExpiryTest', function (req, res) {
  // 获取参数
  let headers = req.headers ? JSON.stringify(req.headers) : '';
  let params = req.params ? JSON.stringify(req.params) : '';
  let query = req.query ? JSON.stringify(req.query) : '';
  let cookieStr = req.headers.cookie ? JSON.stringify(req.headers.cookie) : '';
  console.log('params :----> ', params)
  console.log('query   :----> ', query)
  console.log('headers   :----> ', headers)
  console.log('cookieStr   :----> ', cookieStr)
  var today = new Date();
  var time = today.getTime() + 60 * 1000;
  var time2 = new Date(time);

  // var adds =  ["token=123456789"];
  res.cookie('myCookie', "token=123456789", {
    path: '/',
    httpOnly: true,
    maxAge: 100 * 1000,
    expires:time2,
    signed: false,
    domain: '1.15.87.213',
    secure: false
  })
  res.json({
    header: headers,
    params: params,
    query: query,
    method: req.method,
    url: req.originalUrl,
    msg: '服务器回复'
  })





  // res.setHeader('Set-Cookie', 'token=123456');
  // res.end("end cookie")


  // res.writeHead(200, {
  //   'Set-Cookie': 'myCookie="type=ninja","language=javascript";path="/";Expires=' + timeObj + ';httpOnly=true'
  // });

  // res.end(JSON.stringify({
  //   headers:headers,
  //   params: params,
  //   query: query,
  //   method: req.method,
  //   url: req.originalUrl,
  //   msg: '服务器回复'
  // }))

})

// 访问/getPlus/:id
cookieApi.get('/cookieDomainTest', function (req, res) {
  // 获取参数
  let headers = req.headers ? JSON.stringify(req.headers) : '';
  let params = req.params ? JSON.stringify(req.params) : '';
  let query = req.query ? JSON.stringify(req.query) : '';
  console.log('params :----> ', params)
  console.log('query   :----> ', query)
  console.log('headers   :----> ', headers)
  var today = new Date();
  var time = today.getTime() + 60 * 1000;
  var time2 = new Date(time);
  var timeObj = time2.toGMTString();
  res.writeHead(200,{
    'Set-Cookie': 'myCookie="type=ninja"; language="javascript";path="/";Expires=' + timeObj + ';httpOnly=true;domain=1.15.87.213'
  });
  res.end(JSON.stringify({
    header: headers,
    params: params,
    query: query,
    method: req.method,
    url: req.originalUrl,
    msg: '服务器回复'
  }))

})
module.exports = cookieApi